package com.csi.controller;

import com.csi.domain.User;
import com.csi.domain.vo.LoginVo;
import com.csi.service.UserService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 校验用户名是否存在
     */
    @Operation(summary = "校验用户名是否存在", description = "根据提供的用户名检查用户是否已存在")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "2001", description = "成功响应", content = @Content(schema = @Schema(implementation = Boolean.class)))
    })
    @RequestMapping("validUsername")
    public Object validUsername(
            @Parameter(description = "用户名", required = true, example = "testUser")
            @RequestParam("username") String username) {
        return userService.findByUsername(username) == null;
    }

    /**
     * 校验密码是否正确
     */
    @Operation(summary = "校验密码是否正确", description = "根据用户名和密码检查密码是否正确")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功响应", content = @Content(schema = @Schema(implementation = Boolean.class)))
    })
    @RequestMapping("validPassword")
    public Object validPassword(
            @Parameter(description = "密码", required = true, example = "password123")
            @RequestParam("password") String password,
            @Parameter(description = "用户名", required = true, example = "testUser")
            @RequestParam("username") String username) {
        User user = userService.login(username, password);
        return user != null;
    }

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "用户上传照片并填写信息完成注册")
    @RequestMapping("reg")
    public boolean reg(
            @Parameter(description = "用户头像", required = true)
            @RequestParam("photo") MultipartFile photo,
            @Parameter(description = "用户信息", required = true)
            @ModelAttribute User user) {
        return userService.reg(user, photo);
    }



    /**
     * 用户登录
     */
    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "验证账号密码并生成JWT令牌")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "2001", description = "登录成功", content = @Content(schema = @Schema(implementation = JsonResult.class))),
            @ApiResponse(responseCode = "4001", description = "用户名或密码不正确", content = @Content),
            @ApiResponse(responseCode = "4001", description = "密码错误", content = @Content)
    })
    @RequestMapping("login")
    public JsonResult login(@RequestBody User user) {
        User existingUser = userService.login(user.getUsername(), user.getPassword());
        if (existingUser == null) {
            return new JsonResult(ReturnResults.LOGIN_FAILED, "用户名或密码不正确！");
        } else {
            // 假设userService.login已经验证了密码，因此这里不需要再次验证
            String token = JwtUtil.getToken(existingUser.getUsername(), existingUser.getId());
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(token);
            loginVo.setUserNickname(existingUser.getUserNickname());
            loginVo.setUserHead(existingUser.getUserHead());
            return new JsonResult(ReturnResults.ACCESS_SUCCESS, loginVo, "登录成功！");
        }
    }
}
